package net.chathu.airgw.model 
{
	import flash.utils.Dictionary;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.util.StringUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class UserList 
	{
		//Allowed characters in nick~
		private static const regExp:RegExp = new RegExp("[^a-z^A-Z^0-9^\\-^_^|^(^)^\u00e1^\u00c1^\u00e9^\u00c9^\u00ed^\u00cd^\u00fa^\u00f3^\u00d3^\u00f6^\u00d6^\u0151^\u0150^\u00fa^\u00da^\u00fc^\u00dc^\u0171^\u0170]","g");

		
		private var userdict:Dictionary;
		
		public function UserList() 
		{
			userdict = new Dictionary();
		}
		
		public function add(user:IUser, isItYou:Boolean):Boolean
		{
			if (!contains(user))
			{
				var ircName:String;
				//sajat magunknak semmi esetre sem adjunk prefixet
				if (Config.INCLUDE_AGE_AND_GENDER_IN_NAME && !isItYou && (user is User))
				{
					ircName = User(user).shortExtraInfo + "^" + createIRCCompatibleName(User(user).originalName);
				} else {
					ircName = createIRCCompatibleName(user.originalName);
				}
				
				var counter:int = 2; //ha a nick ~test_user foglalt, test_user2-t probalja eloszor, es igy novelje a szamot
				var prevUser:IUser;
				var tempName:String = ircName;
				
				while((prevUser = getUserByNameIgnoreCase(ircName)) != null)
				{
					ircName = tempName + (counter++);
				}
				
				user.name = ircName;
				
				userdict[user.id] = user;
				return true;
			} else {
				return false;
			}
		}
		
		public function remove(user:IUser):Boolean
		{
			if (contains(user))
			{
				delete userdict[user.id];
				return true;
			} else {
				return false;
			}
		}
		
		public function contains(user:IUser):Boolean
		{
			return !!(userdict[user.id]);
		}
		
		public function clear():void
		{
			userdict = new Dictionary();	
		}
		
		public function getUser(id:*):IUser
		{
			return userdict[id];		
		}
		
		public function getUserByRegID(regid:int):IUser
		{
			for (var key:String in userdict)
			{
				var user:IUser = userdict[key];
				if (user is User)
				{
					if (User(user).regId == regid)
					{
						return user;
					}
				}
			}	
			return null;
		}
				
		public function getUserNum():int
		{
			var n:int = 0;
			for (var key:String in userdict)
			{
				n++;
			}	
			return n;
		}
		
		/* Removed - Csak a gond lenne vele...
		public function getUserByName(name:String):User
		{
			
			for (var key:String in userdict)
			{
				var user:User = userdict[key];
				if (user.name == name)
				{
					return user;
				}
			}	
			return null;
		}*/
		
		public function getUserByNameIgnoreCase(name:String):IUser
		{
			name = name.toLocaleLowerCase();
			for (var key:String in userdict)
			{
				var user:IUser = userdict[key];
				if (user.name.toLocaleLowerCase() == name)
				{
					return user;
				}
			}	
			return null;
		}
		
		
		public static function createIRCCompatibleName(name:String):String
		{
			var temp:String = StringUtil.replaceAll(name, "$", "S");
			temp = StringUtil.replaceAll(name, "@", "(a)");
			temp=temp.replace(regExp, "_");
			return temp; 
		}
		
		public function getDictionary():Dictionary {
			return userdict;
		}
		
	}

}